package com.hbm.usercenter.base;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

import com.hbm.usercenter.R;
import com.hbm.usercenter.factory.ThreadFactory;
import com.hbm.usercenter.utils.LogUtils;
import com.hbm.usercenter.utils.UIUtils;



public abstract class LoadingPager extends FrameLayout {

    private View mLoadingView;
    private View mEmptyView;
    private View mErrorView;
    private View mSuccessView;

    public static final int STATE_NONE = -1;            // 默认状态
    public static final int STATE_LOADING = 0;            // 加载中
    public static final int STATE_ERROR = 1;            // 错误
    public static final int STATE_EMPTY = 2;            // 空
    public static final int STATE_SUCCESS = 3;            // 成功

    public int mCurState = STATE_NONE;

    public LoadingPager(Context context) {
        super(context);
        initCommonView();
    }

    private void initCommonView() {
        mLoadingView = View.inflate(UIUtils.getContext(), R.layout.pager_loading, null);
        this.addView(mLoadingView);
        mEmptyView = View.inflate(UIUtils.getContext(), R.layout.pager_empty, null);
        this.addView(mEmptyView);
        mErrorView = View.inflate(UIUtils.getContext(), R.layout.pager_error, null);
        this.addView(mErrorView);
    }

    private void refreshUIByState() {
        // 控制加载页面的显示/隐藏
        mLoadingView.setVisibility((mCurState == STATE_LOADING) || (mCurState == STATE_NONE) ? View.VISIBLE
                : View.INVISIBLE);

        mErrorView.setVisibility(mCurState == STATE_ERROR ? View.VISIBLE : View.INVISIBLE);

        mEmptyView.setVisibility(mCurState == STATE_EMPTY ? View.VISIBLE : View.INVISIBLE);

        if (mSuccessView == null && mCurState == STATE_SUCCESS) {
            mSuccessView = initSuccessView();
            this.addView(mSuccessView);
        }
        if (mSuccessView != null) {
            mSuccessView.setVisibility(mCurState == STATE_SUCCESS ? View.VISIBLE : View.INVISIBLE);
        }

    }

    public void triggerLoadData() {
        if (mCurState != STATE_SUCCESS && mCurState != STATE_LOADING) {
            LogUtils.sf("###开始加载中");
            // ② 异步加载数据
            // 加载开始前,重置状态为加载中
            int state = STATE_LOADING;
            mCurState = state;
            refreshUIByState();
            // new Thread(new LoadDataTast()).start();
            ThreadFactory.getNormalPool().execute(new LoadDataTast());
        }

    }

    class LoadDataTast implements Runnable {

        @Override
        public void run() {
            int tempState = initData().getState();

            mCurState = tempState;
            UIUtils.postTaskSafely(new Runnable() {

                @Override
                public void run() {
                    refreshUIByState();
                }
            });

        }
    }

    protected abstract LoadedResult initData();

    protected abstract View initSuccessView();

    public enum LoadedResult {
        SUCCESS(STATE_SUCCESS), EMPTY(STATE_EMPTY), ERROR(STATE_ERROR);

        int mState;

        public int getState() {
            return mState;
        }

        private LoadedResult(int state) {
            mState = state;
        }
    }

}
